package org.jeecg.modules.engine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.engine.entity.Frame;
import org.jeecg.modules.engine.entity.Task;
import org.jeecg.modules.engine.entity.vo.FramePhoneVo;
import org.jeecg.modules.engine.entity.vo.FrameProcPhoneVo;
import org.jeecg.modules.engine.entity.vo.FrameTotalVo;
import org.jeecg.modules.engine.entity.vo.FrameVo;
import org.jeecg.modules.engine.mapper.FrameMapper;
import org.jeecg.modules.engine.mapper.TaskMapper;
import org.jeecg.modules.engine.service.IFrameService;
import org.jeecg.modules.flow.entity.FlowDetail;
import org.jeecg.modules.flow.mapper.FlowDetailMapper;
import org.jeecg.modules.procedure.entity.Proc;
import org.jeecg.modules.procedure.mapper.ProcMapper;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 构件表
 * @Author: jeecg-boot
 * @Date:   2020-07-20
 * @Version: V1.0
 */
@Service
public class FrameServiceImpl extends ServiceImpl<FrameMapper, Frame> implements IFrameService {

	@Autowired
	private FrameMapper frameMapper;

	@Autowired
	private TaskMapper taskMapper;

	@Autowired
	private FlowDetailMapper flowDetailMapper;

	@Autowired
	private ProcMapper procMapper;
	@Override
	public List<Frame> selectByMainId(String mainId) {
		return frameMapper.selectByMainId(mainId);
	}

	@Override
	public IPage<FrameVo> selectFramePage(Page<FrameVo> page, FrameVo frameVo) {
		return frameMapper.selectFramePage(page,frameVo);
	}

	@Override
	public IPage<Frame> framePage(Page<Frame> page, Frame frame) {
		return frameMapper.framePage(page,frame);
	}

	@Override
	public FrameTotalVo frameTotal(Frame frame) {
		return frameMapper.frameTotal(frame);
	}

	@Override
	public IPage<FramePhoneVo> inPhoneList(Page<FramePhoneVo> page, FramePhoneVo framePhoneVo) {
		return baseMapper.inPhoneList(page,framePhoneVo);
	}

	@Override
	public IPage<FrameProcPhoneVo> inPhoneFrameList(Page<FrameProcPhoneVo> page, FrameProcPhoneVo frameProcPhoneVo) {
		return baseMapper.inPhoneFrameList(page,frameProcPhoneVo);
	}

	@Override
	public Result<?> openFrame(Frame frame) {
		Frame updateFrame = new Frame();
		updateFrame.setId(frame.getId());
		updateFrame.setIsEnable(frame.getIsEnable());
		int updateCount = baseMapper.updateById(updateFrame);
		if (updateCount>0){
			return Result.ok("更新成功");
		}
		return Result.error("更新失败");
	}

	@Override
	public FrameVo findById(String id) {
		FrameVo frameVo = baseMapper.findById(id);
		frameVo.setTasks(taskMapper.selectByMainId(id));
		return frameVo;
	}

	@Override
	public Boolean addTask(Frame frame) {
		Frame f = baseMapper.selectOne(new LambdaQueryWrapper<Frame>().eq(Frame::getId, frame.getId()));
		if ((f.getFlowId()!=null && !f.getFlowId().equals("")) || (f.getFlowName() != null && !f.getFlowName().equals(""))){
			return false;
		}
		Frame updateFrame = new Frame();
		updateFrame.setId(frame.getId());
		updateFrame.setFlowId(frame.getFlowId());
		updateFrame.setFlowName(frame.getFlowName());
		frameMapper.updateById(updateFrame);
		List<FlowDetail> flowDetails = flowDetailMapper.selectList(new LambdaQueryWrapper<FlowDetail>().eq(FlowDetail::getFlowId, frame.getFlowId()));
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String currDay = simpleDateFormat.format(new Date());
		final int[] count = {taskMapper.selectCount(new LambdaQueryWrapper<Task>().ge(Task::getCreateTime, currDay + " 00:00:00").and(p -> p.le(Task::getCreateTime, currDay + " 23:59:59")))};
		String prefix = "WK";
		String dateFormat = new SimpleDateFormat("yyyyMMdd").format(new Date());
		List<String> procIds = flowDetails.stream().map(FlowDetail::getProcId).collect(Collectors.toList());
		List<Proc> procList = procMapper.selectList(new LambdaQueryWrapper<Proc>().in(Proc::getId, procIds));
		flowDetails.forEach(flowDetail -> {
			Task task = new Task();
			count[0]=count[0]+1;
			String code = prefix+dateFormat+String.format("%04d", count[0]);
			task.setTaskNo(code);
			task.setFramId(frame.getId());
			task.setProcId(flowDetail.getProcId());
			task.setHandleSort(flowDetail.getSort());
			Proc currProc = procList.stream().filter(proc -> proc.getId().equals(flowDetail.getProcId())).findFirst().get();
			task.setProcCode(currProc.getCode());
			task.setProcName(currProc.getName());
			task.setTaskCount(f.getCount());
			if (flowDetail.getSort() == 1){
				task.setInProcCount(f.getCount());
			}else {
				task.setInProcCount(0);
			}
			task.setEligibilityCount(0);
			task.setReportCount(0);
			taskMapper.insert(task);
		});
		return true;
	}

	@Override
	public Boolean deleteFrame(String id) {
		Frame frame = baseMapper.selectOne(new LambdaQueryWrapper<Frame>().eq(Frame::getId, id));
		if (frame.getStatus() == 1){
			int deleteCount = baseMapper.deleteById(id);
			taskMapper.delete(new LambdaQueryWrapper<Task>().eq(Task::getFramId,frame.getId()));
			if (deleteCount>0) return true;
		}
		return false;
	}

	@Override
	public Result deleteBatchFrame(String ids) {
		List<Frame> frameList = baseMapper.selectBatchIds(Arrays.asList(ids.split(",")));
		if (frameList.stream().filter(p->p.getStatus()==1).count() == frameList.size()){
			int deleteBatchCount = baseMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
			if (deleteBatchCount == frameList.size()) return Result.ok("批量删除成功！");
		}
		return Result.error("你选择的构件，存在已报工的数据，不能批量删除！");
	}
}
